﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using GreatB2C.Config.SiteInfo;
using GreatB2C.Utility;

namespace GreatB2C.Web.Manage.SystemSet.SiteInfo
{
    public partial class MailParams : Inherit.ManagePage
    {
        protected void Page_Load( object sender , EventArgs e )
        {
            if ( !IsPostBack )
            {
                LoadSetting( );
            }
        }

        protected void LoadSetting( )
        {
            txtMailFrom.Text = EmailConfig.MailFrom.ToString( );
            chkCollocateCheckBox.Checked = Convert.ToBoolean( Convert.ToInt32( EmailConfig.CollocateCheckBox ) );
            txtMailServer.Text = EmailConfig.MailServer.ToString( );
            txtPort.Text = EmailConfig.Port.ToString( );
            chkSSL.Text = EmailConfig.SSL.ToString( );
            string Credential = EmailConfig.Credential.ToString( );
            if ( Credential == "1" )
            {
                radNone.Checked = true;
                PalBasic.Enabled = false;
            }
            else if ( Credential == "2" )
            {
                radBasic.Checked = true;
                PalBasic.Enabled = true;
            }
            else if ( Credential == "3" )
            {
                radNTLM.Checked = true;
                PalBasic.Enabled = false;
            }
            txtMailServerUserName.Text = EmailConfig.MailUserName.ToString( );
            txtMailServerPassWord.Text = EmailConfig.MailPassword.ToString( );
        }

        protected void BtnSubmit_Click( object sender , EventArgs e )
        {
            int Credential = 1;
            if ( radBasic.Checked )
            {
                Credential = 2;
            }
            else if ( radNTLM.Checked )
            {
                Credential = 3;
            }

            bool isSuccess = EmailConfig.Config( txtMailFrom.Text.Trim( ) , Convert.ToInt32( chkCollocateCheckBox.Checked ).ToString( ) , txtMailServer.Text.Trim( )
                , txtPort.Text.Trim( ) , chkSSL.Text.Trim( ) , Credential.ToString( ) , txtMailServerUserName.Text.Trim( ) , txtMailServerPassWord.Text.Trim( ) );

            if ( isSuccess )
            {
                Response.Redirect( "../../Message.aspx?msg=" + Server.UrlEncode( "邮件参数配置成功！" ) + "&rtn=SystemSet/SiteInfo/MailParams.aspx" );
            }
            else
            {
                Response.Redirect( "../../Message.aspx?msg=" + Server.UrlEncode( "邮件参数配置失败，请联系管理员！" ) + "&rtn=SystemSet/SiteInfo/MailParams.aspx" );
            }
        }

        protected void BtnTest_Click( object sender , EventArgs e )
        {
            EmailParams email = new EmailParams( );
            email.ToEmailAddress = txtTestMail.Text.Trim( );
            email.FromEmailAddress = EmailConfig.MailFrom.ToString( );
            email.FromEmailPassword = EmailConfig.MailPassword.ToString( );
            email.EmailSubject = "测试邮件";
            email.EmailBody = "您好，这封邮件是长城商城后台管理发出的测试邮件，测试邮箱是否可用！";
            email.EmailPersonName = "admin";
            email.EmailHostName = EmailConfig.MailServer.ToString( );
            email.EmailPriority = "normal";
            email.EmailPort = Convert.ToInt32( EmailConfig.Port );
            email.EncodingType = "default";

            bool isSuccess = EmailHelper.SendingEmail( email );
            if ( isSuccess )
            {
                JScript.Alert( "测试邮件已经发送成功，请及时查阅！" );
            }
            else
            {
                JScript.Alert( "测试邮件发送失败！" );
            }
        }
    }
}
